package com.magicsoft.share.base;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.gyf.barlibrary.ImmersionBar;
import com.magicsoft.share.constant.Constant;

import butterknife.ButterKnife;
import butterknife.Unbinder;

import static android.app.Activity.RESULT_OK;

/**
 * Created by lyf on 2017/8/16.
 */

public abstract class BaseFragment<V extends BaseView, T extends BasePresenter<V>> extends Fragment {

    public T mPresenter;
    public View mView;
    private Unbinder unbinder;
    protected ImmersionBar mImmersionBar;
    private boolean isImmersionBarEnabled=false;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mView = inflater.inflate(getLayoutId(), container, false);

        if (isImmersionBarEnabled()) {
            initImmersionBar();
        }

        unbinder = ButterKnife.bind(this, mView);
        mPresenter = initPresenter();
        if (mPresenter != null) {
            mPresenter.attach((V) this);
        }
        initData();
        initView();

        return mView;
    }




    @Override
    public void onResume() {
        super.onResume();
        requestData();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (mPresenter != null) {
            mPresenter.detach();
        }
        if (mImmersionBar != null) {
            mImmersionBar.destroy();
        }
        unbinder.unbind();
    }


    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden && mImmersionBar != null) {
            mImmersionBar.init();
        }
    }

    /**
     * 是否在Fragment使用沉浸式
     *
     * @return the boolean
     */
    protected boolean isImmersionBarEnabled() {
        return isImmersionBarEnabled;
    }
    public void setImmersionBarEnabled(boolean isImmersionBarEnabled) {
        this.isImmersionBarEnabled= isImmersionBarEnabled;
    }

    /**
     * 初始化沉浸式
     */
    protected void initImmersionBar() {
        mImmersionBar = ImmersionBar.with(this).statusBarDarkFont(true);
        mImmersionBar.keyboardEnable(true).navigationBarWithKitkatEnable(false).init();
    }

    @LayoutRes
    public abstract int getLayoutId();

    public abstract T initPresenter();

    protected abstract void initData();
    public abstract void initView();
    protected abstract void requestData();


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {

            Log.d("lyf", "onActivityResult: ");
            if (requestCode == Constant.REFRESH_UI) {
                requestData();
            }
        }
    }
}
